# -*- coding: utf-8 -*-
"""
@Time ： 2023/4/7 21:37
@Auth ： 叶东宇
@File ：Chain.py
"""

"""
单链表
"""


class Node:
    def __init__(self, value_=0, next_=None):
        """
        :param value: 结点数据
        :param next: 结点指针
        """
        self.value = value_
        self.next = next_


class Chain:
    def __init__(self):
        # 头指针
        self.head = Node()
        # 头指针
        self.head_point = None
        # 尾指针
        self.tail_point = None

    def addNodeToHead(self, value):
        """
        头插法
        :param node:
        :return:
        """
        node = Node(value, None)
        tmp = self.head.next
        self.head.next = node
        node.next = tmp
        if self.head_point is None:
            self.head_point = node
        self.tail_point = node

    def addNodetoTail(self, value):
        """
        尾插法
        :param node:
        :return:
        """
        node = Node(value, None)
        if self.tail_point is None:
            self.head.next = node
            self.head_point = node
            self.tail_point = node
        else:
            self.tail_point.next = node
            self.tail_point = node

    def traverse(self):
        """
        遍历LinkedNode
        :return: list
        """
        lis = []
        node = self.head
        while node.next is not None:
            lis.append(node.next.value)
            node = node.next
        return lis


def create_chain_head(lis):
    """
    抽取的方法，直接更具list生成链表
    :param lis:
    :return:
    """
    chain = Chain()
    if len(lis) == 0:
        return chain
    else:
        for i in lis:
            chain.addNodeToHead(i)
        return chain


if __name__ == "__main__":
    lis = [0, 3, 4, 5, 6, 7, 8, 9, 9, 10]
    chain = create_chain_head(lis)
    print(chain.traverse())
